require.config({
    paths: {
        'vue': '../encaptool/encvue',
        'iview': '../utiltool/iview/iview',
        'jquery': '../utiltool/jquery/jquery.min',
        'router': '../encaptool/encarouter',
        'axios': '../encaptool/encaxios'
    },
    shim: {
        iview: [
            'vue'
        ]
    }
})
require(['vue', 'iview', 'jquery', 'router', 'axios'], function(Vue, iview, $, Router, axios) {
    Vue.use(iview);
    new Vue({
        el: ".production_order",
        data() {
            return {
                pageloading: false,
                customerList: [],
                bournList: [],
                boardList: [],
                fluteList: [],
                formatList: [],
                orderidList: [],
                // post数据
                customer: "香河三隆制品有限公司",
                bourn: "香河县安头屯镇北口头村",
                board: "G4C",
                flute: "A",
                format: "平板（外径2400*1400)",
                truckload: 100,
                area: 336.00,
                oneofarea: 3.36,
                coefficient: 0.004100,
                numberofplies: 3.00,
                volume: 30.00,
                orderid: (function() {
                    var adate = new Date();
                    var year = adate.getFullYear().toString().substr(2, 2);
                    var month = adate.getMonth().toString();
                    var day = adate.getDate().toString();
                    if (adate.getMonth() < 10) {
                        month = "0" + (adate.getMonth() + 1).toString();
                    }
                    if (adate.getDate() < 10) {
                        day = "0" + adate.getDate().toString();
                    }
                    var time = adate.getHours().toString() + adate.getMinutes().toString();
                    return year + month + day + time;
                })(),
                // 界面绑定属性
                tabName: "name1",
                // 拆单表格选项
                TBDcolumns: [{
                    type: 'index',
                    align: 'center',
                    width: 50,
                    align: "center"
                }, {
                    width: 120,
                    title: '订单编号',
                    key: 'orderid',
                    align: "center"
                }, {
                    width: 250,
                    title: '客户名称',
                    key: 'customer',
                    align: "center"
                }, {
                    width: 250,
                    title: '送货地址',
                    key: 'bourn',
                    align: "center"
                }, {
                    width: 80,
                    title: '板材',
                    key: 'board',
                    align: "center"
                }, {
                    width: 110,
                    title: '规格（楞型）',
                    key: 'flute',
                    align: "center"
                }, {
                    width: 170,
                    title: '规格型号',
                    key: 'format',
                    align: "center"
                }, {
                    width: 100,
                    title: '面积',
                    key: 'area',
                    align: "center"
                }, {
                    width: 100,
                    title: '单位面积',
                    key: 'oneofarea',
                    align: "center"
                }, {
                    width: 60,
                    title: '层数',
                    key: 'numberofplies',
                    align: "center"
                }, {
                    width: 140,
                    title: '体积',
                    key: 'volume',
                    align: "center"
                }, {
                    width: 120,
                    title: '装载量',
                    key: 'truckload',
                    align: "center"
                }, {
                    width: 120,
                    title: '运费系数',
                    key: 'coefficient',
                    align: "center"
                }],
                // 合单表格选项
                TBDcolumns_merge: [{
                    type: 'selection',
                    width: 50,
                    align:"center"
                }, {
                    width: 105,
                    title: '订单编号',
                    key: 'orderid',
                    align:"center"
                }, {
                    width: 230,
                    title: '客户名称',
                    key: 'customer',
                    align: "center"
                }, {
                    width: 320,
                    title: '送货地址',
                    key: 'bourn',
                    align: "center"
                }, {
                    width: 70,
                    title: '板材',
                    key: 'board',
                    align: "center"
                }, {
                    width: 110,
                    title: '规格（楞型）',
                    key: 'flute',
                    align: "center"
                }, {
                    width: 170,
                    title: '规格型号',
                    key: 'format',
                    align: "center"
                }, {
                    width: 120,
                    title: '面积',
                    key: 'area',
                    align: "center"
                }, {
                    width: 120,
                    title: '单位面积',
                    key: 'oneofarea',
                    align: "center"
                }, {
                    width: 70,
                    title: '层数',
                    key: 'numberofplies',
                    align: "center"
                }, {
                    width: 120,
                    title: '体积',
                    key: 'volume'
                }, {
                    width: 110,
                    title: '装载量',
                    key: 'truckload'
                }, {
                    width: 120,
                    title: '运费系数',
                    key: 'coefficient'
                }],
                // 合并表选项
                mergeCloums: [{
                    title: '总面积',
                    key: 'allArea',
                    align: 'center'
                }, {
                    title: '总体积',
                    key: 'allVolume',
                    align: 'center'
                }, {
                    title: '总装载量',
                    key: 'allTruck',
                    align: 'center'
                }],
                // 合并表数据
                mergeData: [{
                    allArea: 0,
                    allVolume: 0,
                    allTruck: 0
                }],
                mergeId: [],
                // 表格数据
                TBDdata: [],
                // 拆单显示数据
                removeShow: {},
                truckloadMax: 0,
                truckloadNow: 0,
                oneofvolume: 0,
                afterTruck: 0,
                afterVloume: 0,
                afterArea: 0,
                newRemoveOrder: "",
                // 拆分订单步骤
                removeCurrent: 0,
                tableShow: true,
                removing: false,
                removeHasData: false,
                removeSuccess: false,
                // 合成订单步骤
                mergeCurrent: 0,
                mergeTableShow: true,
                mergeTruckList: false,
                mergeSuccess: false,
                // 配送选车数据
                Truckcolumns: [{
                    type: 'index',
                    align: 'center'
                }, {
                    title: '订单编号',
                    key: 'truckid'
                }, {
                    title: '周六运输量',
                    key: 'Saturday',
                    sortable: true
                }, {
                    title: '周日运输量',
                    key: 'Sunday',
                    sortable: true
                }, {
                    title: '节假日运单数',
                    key: 'frequency',
                    sortable: true
                }, {
                    title: '最大装载体积',
                    key: 'volume',
                    sortable: true
                }],
                truckid: '',
                truckList: [],
                showTruckid: '',
                // 手动配送
                manualShow: false,
                // 自动配送
                autoTable: false,
                autoColumns: [{
                    type: 'index',
                    align: 'center'
                }, {
                    title: '总体积',
                    key: 'processed_volume'
                }, {
                    title: '等待可能',
                    key: 'probability'
                }],
                isEmergency: false,
                autoShow: false,
                autoData: [],
                autoBanner: '0',
                // mianshow
                mainShow: true
            }
        },
        watch: {
            customer: 'getCustomerList',
            bourn: 'getBournList',
            board: 'getBoardList',
            flute: 'getFluteList',
            format: 'getFormatList',
            orderid: 'getOrderList',
            truckloadNow: function(val) {
                this.removeShow.area = val * this.removeShow.oneofarea;
                this.removeShow.volume = val * this.oneofvolume;
                this.afterTruck = this.truckloadMax - val;
                this.afterArea = this.afterTruck * this.removeShow.oneofarea;
                this.afterVloume = this.afterTruck * this.oneofvolume;
            },
            area: function(val) {
                this.truckload = parseFloat((this.area / this.oneofarea).toFixed(2)); 
                if (this.oneofarea==0) {
                    this.truckload=0;
                }            
            },
            oneofarea: function(val) {
                this.truckload = parseFloat((this.area / this.oneofarea).toFixed(2)); 
                if (this.area==0) {
                    this.truckload=0;
                }            
            }
        },
        mounted() {
            this.getTableDate();
            this.getTruckList();
        },
        methods: {
            clickManual: function(name) {
                var tab1 = $(".main").height()+$(".ivu-table-body").height()+300;
                var tab2 = 1030;
                var tab3 = $(".ivu-table-body").height()+$(".ivu-table-wrapper").height()+260;
                if (name == "name1") {
                    $(".ivu-tabs").height(tab1);
                }
                if (name == "name2") {
                    $(".ivu-tabs").height(tab2);
                }
                else if (name == "name3") {
                    $(".ivu-tabs").height(tab3);
                }
            },
            getNewList: function() {
                var setting = {
                    method: 'GET',
                    url: "/YF/public/api/getNewList/"
                }
                axios.call(this, function(res) {
                    if (res.code === 0) {
                        var data = res.data;
                        this.area = parseFloat(data[0]['area']);
                        this.board = data[0]['board'];
                        this.bourn = data[0]['bourn'];
                        this.coefficient = parseFloat(data[0]['coefficient']);
                        this.customer = data[0]['customer'];
                        this.flute = data[0]['flute'];
                        this.format = data[0]['format'];
                        this.orderid = data[0]['orderid'];
                        this.numberofplies = parseFloat(data[0]['numberofplies']);
                        this.oneofarea = parseFloat(data[0]['oneofarea']);
                        this.truckload = parseFloat(data[0]['truckload']);
                        this.volume = parseFloat(data[0]['volume']);
                    }
                    this.pageloading = false;
                }.bind(this), {
                    setting
                });
            },
            getCustomerList(val, oldVal) {
                var setting = {
                    method: 'POST',
                    url: "/YF/public/api/getCustomerList/"
                }
                var data = {
                    customer: val
                }
                if (val) {
                    axios.call(this, function(res) {
                        if (res.code === 0) {
                            var data = res.data;
                            this.customerList = data.slice(0, 10);
                        }
                    }.bind(this), {
                        setting,
                        data
                    });
                }
            },
            getBournList(val, oldVal) {
                var setting = {
                    method: 'POST',
                    url: "/YF/public/api/getBournList/"
                }
                var data = {
                    bourn: val
                }
                if (val) {
                    axios.call(this, function(res) {
                        if (res.code === 0) {
                            var data = res.data;
                            this.bournList = data.slice(0, 10);
                        }
                    }.bind(this), {
                        setting,
                        data
                    });
                }
            },
            getBoardList(val, oldVal) {
                var setting = {
                    method: 'GET',
                    url: "/YF/public/api/getBoardList/" + val
                }
                if (val) {
                    axios.call(this, function(res) {
                        if (res.code === 0) {
                            var data = res.data;
                            this.boardList = data.slice(0, 10);
                        }
                    }.bind(this), {
                        setting
                    });
                }
            },
            getFluteList(val, oldVal) {
                var setting = {
                    method: 'GET',
                    url: "/YF/public/api/getFluteList/" + val
                }
                if (val) {
                    axios.call(this, function(res) {
                        if (res.code === 0) {
                            var data = res.data;
                            this.fluteList = data.slice(0, 10);
                        }
                    }.bind(this), {
                        setting
                    });
                }
                switch (this.flute) {
                    case "A":
                        this.coefficient = 0.0041;
                        break;
                    case "B":
                        this.coefficient = 0.0026;
                        break;
                    case "BA":
                        this.coefficient = 0.0067;
                        break;
                    case "-BA":
                        this.coefficient = 0.0061;
                        break;
                    case "BC":
                        this.coefficient = 0.006;
                    case "C":
                        this.coefficient = 0.0034;
                        break;
                    case "E":
                        this.coefficient = 0.0017;
                        break;
                    case "EB":
                        this.coefficient = 0.0043;
                        break;
                    case "-EB":
                        this.coefficient = 0.004;
                        break;
                    default:
                        break;
                }
            },
            getFormatList(val, oldVal) {
                var setting = {
                    method: 'GET',
                    url: "/YF/public/api/getFormatList/" + val
                }
                if (val) {
                    axios.call(this, function(res) {
                        if (res.code === 0) {
                            var data = res.data;
                            this.formatList = data.slice(0, 10);
                        }
                    }.bind(this), {
                        setting
                    });
                }
            },
            getOrderList(val, oldVal) {
                var setting = {
                    method: 'GET',
                    url: "/YF/public/api/getOrderList/" + val
                }
                if (val) {
                    axios.call(this, function(res) {
                        if (res.code === 0) {
                            var data = res.data;
                            this.orderidList = data.slice(0, 10);
                        }
                    }.bind(this), {
                        setting
                    });
                }
                if (val.length === 10) {
                    var setting = {
                        method: 'GET',
                        url: "/YF/public/api/getOneInfoByOrderid/" + val
                    }
                    if (val) {
                        axios.call(this, function(res) {
                            if (res.code === 0) {
                                var data = res.data;
                                this.area = parseFloat(data['area']);
                                this.board = data['board'];
                                this.bourn = data['bourn'];
                                this.coefficient = parseFloat(data['coefficient']);
                                this.customer = data['customer'];
                                this.flute = data['flute'];
                                this.format = data['format'];
                                this.orderid = data['orderid'];
                                this.numberofplies = parseFloat(data['numberofplies']);
                                this.oneofarea = parseFloat(data['oneofarea']);
                                this.truckload = parseFloat(data['truckload']);
                                this.volume = parseFloat(data['volume']);
                                if (isNaN(this.volume)) {
                                    this.volume = 0;
                                }
                            }
                        }.bind(this), {
                            setting
                        });
                    }

                }
            },
            addOrder() {
                var setting = {
                    method: 'POST',
                    url: "/YF/public/api/AddOrder"
                };
                if (this.customer === "" || this.board === "" || this.board === "" || this.flute === "" || this.format === "") {
                    this.$Message.warning('请输入全部信息。');
                    return false;
                }
                var data = {
                    orderid: this.orderid,
                    customer: this.customer,
                    bourn: this.bourn,
                    board: this.board,
                    flute: this.flute,
                    format: this.format,
                    truckload: this.truckload,
                    area: this.area,
                    oneofarea: this.oneofarea,
                    coefficient: this.coefficient,
                    numberofplies: this.numberofplies,
                    volume: this.volume
                }
                axios.call(this, function(res) {
                    if (res.code === 0 || res.data === 1) {
                        this.$Message.success('添加成功');
                        this.getTableDate();
                    } else {
                        this.$Message.info('出现不可预期的错误');
                    }
                }.bind(this), {
                    setting,
                    data
                });
            },
            getTableDate(val) {
                if (val == "name1") {
                    return false;
                }
                var setting = {
                    method: 'GET',
                    url: "/YF/public/api/getTBDOrder"
                }
                axios.call(this, function(res) {
                    if (res.code === 0) {
                        this.TBDdata = res.data;
                    } else {
                        this.TBDdata = [];
                    }
                }.bind(this), {
                    setting
                });
            },
            onRemove(selection) {
                this.removeShow = selection;
                this.truckloadMax = parseInt(this.removeShow.truckload);
                this.truckloadNow = parseInt(this.removeShow.truckload);
                this.oneofvolume = parseFloat(this.removeShow.volume / this.removeShow.truckload);
                this.newRemoveOrder = '当前拆分订单:' + this.removeShow.orderid;
                this.removeHasData = true;
            },
            removePre() {
                this.tableShow = true;
                this.removing = false;
                this.removeCurrent = 0;
            },
            removeNext() {
                if (this.removeCurrent === 0) {
                    if (this.removeHasData) {
                        this.removeCurrent = 1;
                        this.tableShow = false;
                        this.removing = true;
                    } else {
                        this.$Message.warning('请选择订单');
                    }
                } else if (this.removeCurrent === 1) {
                    if (this.afterTruck === 0) {
                        this.$Message.warning('新订单装载量不能为0')
                        return false;
                    }
                    var afterData = {
                        customer: this.removeShow.customer,
                        bourn: this.removeShow.bourn,
                        board: this.removeShow.board,
                        flute: this.removeShow.flute,
                        format: this.removeShow.format,
                        truckload: this.truckloadNow,
                        area: this.removeShow.area,
                        oneofarea: this.removeShow.oneofarea,
                        coefficient: this.removeShow.coefficient,
                        numberofplies: this.removeShow.numberofplies,
                        volume: this.removeShow.volume
                    };
                    var nowData = {
                        customer: this.removeShow.customer,
                        bourn: this.removeShow.bourn,
                        board: this.removeShow.board,
                        flute: this.removeShow.flute,
                        format: this.removeShow.format,
                        truckload: this.afterTruck,
                        area: this.afterArea,
                        oneofarea: this.removeShow.oneofarea,
                        coefficient: this.removeShow.coefficient,
                        numberofplies: this.removeShow.numberofplies,
                        volume: this.afterVloume
                    };
                    var setting = {
                        method: 'POST',
                        url: "/YF/public/api/splitOrder"
                    };
                    var data = {
                        afterData: afterData,
                        nowData: nowData,
                        oldId: this.removeShow.id
                    }
                    axios.call(this, function(res) {
                        if (res.code === 0 || res.data === 1) {
                            this.removeCurrent = 2;
                            this.removing = false;
                            this.removeSuccess = true;
                            this.newRemoveOrder = "";
                            this.getTableDate();
                        } else {
                            this.$Message.info('出现不可预期的错误');
                        }
                    }.bind(this), {
                        setting,
                        data
                    });
                }
            },
            initRemove() {
                this.removeCurrent = 0;
                this.tableShow = true;
                this.removing = false;
                this.removeHasData = false;
                this.removeSuccess = false;
            },
            mergeAllOrder(selection) {
                this.mergeData[0].allArea = 0;
                this.mergeData[0].allVolume = 0;
                this.mergeData[0].allTruck = 0;
                this.mergeId = [];
                selection.forEach(s => {
                    this.mergeId.push(s.id);
                    this.mergeData[0].allArea += parseFloat(s.area);
                    this.mergeData[0].allVolume += parseFloat(s.volume);
                    this.mergeData[0].allTruck += parseFloat(s.truckload);
                });
            },
            getTruckList() {
                var setting = {
                    method: 'GET',
                    url: "/YF/public/api/getTruckList"
                }
                axios.call(this, function(res) {
                    if (res.code === 0) {
                        this.truckList = res.data;
                    }else{
                        this.truckList = [];
                    }
                }.bind(this), {
                    setting
                });
            },
            // 待完成
            toMergeOrder() {
                if (this.mergeCurrent === 0 && (this.mergeId !== undefined  &&  this.mergeId.length > 0 )) {
                    this.mainShow = false;
                    this.manualShow = true;
                    this.mergeCurrent = 1;
                } else if (this.mergeCurrent === 1 ) {
                    var setting = {
                        method: 'POST',
                        url: "/YF/public/api/produceFreightId"
                    };
                    
                    var data = {
                        mergeId: this.mergeId,
                        truckid: this.truckid,
                        isEmergency: this.isEmergency
                    }
                    axios.call(this, function(res) {
                        if (res.code === 0 || res.data === 1) {
                            this.mergeCurrent = 2;
                            this.mergeSuccess = true;
                            this.manualShow = false;
                        } else {
                            this.$Message.info('出现不可预期的错误');
                        }
                    }.bind(this), {
                        setting,
                        data
                    });
                } else {
                    this.$Message.warning("请选择!");
                }
            },
            initMerge() {
                this.mergeCurrent = 0;
                this.mainShow = true;
                this.mergeSuccess = false;
                this.getTableDate();
            },
            toSelectCat() {
                this.initMerge();
                this.initRemove();
                this.tabName = "name4";
            },
            selectTruck(selection, row) {
                this.truckid = selection.truckid;
                this.showTruckid = "当前选择:" + this.truckid;
            },
            mergePre() {
                this.mergeId = "";
                this.truckid = "";
                this.mergeTableShow = true;
                this.mergeTruckList = false;
                this.mergeCurrent = 0;
                this.getTableDate();
            },
            manual() {
                this.mainShow = true;
                this.manualShow = true;
            },
            selectInAuto() {
                this.autoTable = true;
                this.mainShow = true;
            },
            auto() {
                this.pageloading = true;
                
                this.mainShow = false;
                this.autoShow = true;
                var setting = {
                    method: 'POST',
                    url: "/YF/public/api/autoProduction",
                    timeout: 30000*10
                }
                axios.call(this, function(res) {
                    if (res.code === 0) {
                        this.autoData = res.data;
                        this.pageloading = false;
                        this.mainShow = false;
                        this.autoShow = true;
                    } else {
                        this.pageloading = false;
                        this.$Message.warning("当前没有订单!");
                    }

                }.bind(this), {
                    setting
                });

            },

            backMain() {
                this.manualShow = false;
                this.mergeSuccess = false;
                this.autoShow = false;
                this.mainShow = true;
                this.mergeCurrent = 0;
            },
            backMainSecond() {
                this.autoShow = false;
                this.mainShow = true;
                this.autoTable = false;
                this.manualShow = true;
            },
            autoSuccess(num) {
                var setting = {
                    method: 'POST',
                    url: "/YF/public/api/produceFreightId"
                };

                var data = {
                    mergeId: this.autoData[num].id,
                    truckid: this.autoData[num].truckid
                }
                axios.call(this, function(res) {
                    if (res.code === 0 || res.data != 0) {
                        this.autoData.splice(num, 1);
                    } else {
                        this.$Message.info('出现不可预期的错误');
                    }
                }.bind(this), {
                    setting,
                    data
                });
            }


        }
    })
})